package main

import (
	"fmt"
	"log"
	// "os"
	"time"

	"github.com/lazywei/go-opencv/opencv"
)

func main() {
	win := opencv.NewWindow("GetFace")
	defer win.Destroy()

	cap := opencv.NewCameraCapture(0)
	if cap == nil {
		log.Panicln("Open Camera Failed")
		return
	}
	defer cap.Release()

	cascade := opencv.LoadHaarClassifierCascade("harr/haarcascade_frontalface_alt.xml")

	for {

		image := cap.QueryFrame()
		image = opencv.Resize(image, image.Width()/2, image.Height()/2, opencv.CV_INTER_CUBIC)
		if image == nil {
			log.Panicln("Get Image Failed")
			break
		}

		faces := cascade.DetectObjects(image)

		if len(faces) == 1 {
			fmt.Println("Get a Face")

			imagename := fmt.Sprintf("face/%d.jpg", time.Now().Nanosecond())

			// 保存图片
			opencv.SaveImage(imagename, image, 95)

			// 上传图片到七牛
			// 写入数据库

			return

		}

		win.ShowImage(image)

	}
}
